package zarzadzaniepracownikami;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class MainWindow extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3856170848093197995L;
	private ServerConnector connector= new ServerConnector();
	private JFrame frmWorkerMenagment;
	private JList workerList;
	private Vector<Worker> listOfWorker;
	private Worker selectedWorker;
	private DefaultListModel listOfWorkreModel;
	private JButton btnSeeSalary;
	private JButton btnAddWorker;
	private JButton btnRemoveWorker;
	private JButton btnViewProfile;
	private JButton btnMenagePositon;
	private AddWorker addWorker;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWindow window = new MainWindow();
					window.frmWorkerMenagment.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainWindow() {
		
		selectedWorker = new Worker();
		
		listOfWorker = connector.GetListOfWorkers();
		
		listOfWorkreModel = new DefaultListModel();
				
		CreatList();
		
		initialize();
	}

	private void CreatList()
	{
		if(listOfWorker.size()!=0) listOfWorkreModel.removeAllElements();
		
		for (Worker worker : listOfWorker) {
			listOfWorkreModel.addElement(worker.getId()+" "+worker.getName()+" "+ worker.getSurname()+ " "+worker.getPosition());
		}
	}
	
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmWorkerMenagment = new JFrame();
		frmWorkerMenagment.setTitle("Zarządzanie pracownikami");
		frmWorkerMenagment.setBounds(100, 100, 529, 507);
		frmWorkerMenagment.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmWorkerMenagment.getContentPane().setLayout(null);
		
		
		btnAddWorker = new JButton("Dodaj pracownika");
		btnAddWorker.setBounds(327, 30, 170, 25);
		btnAddWorker.addActionListener(this);
		frmWorkerMenagment.getContentPane().add(btnAddWorker);
		
		btnRemoveWorker = new JButton("Usuń pracownika");
		btnRemoveWorker.setEnabled(false);
		btnRemoveWorker.setBounds(327, 78, 170, 25);
		btnRemoveWorker.addActionListener(this);
		frmWorkerMenagment.getContentPane().add(btnRemoveWorker);
		
		btnSeeSalary = new JButton("Podejrzyj pensje");
		btnSeeSalary.setEnabled(false);
		btnSeeSalary.setBounds(327, 127, 170, 25);
		btnSeeSalary.addActionListener(this);
		frmWorkerMenagment.getContentPane().add(btnSeeSalary);
		
		btnViewProfile = new JButton("Zobacz profil");
		btnViewProfile.setEnabled(false);
		btnViewProfile.setBounds(327, 176, 170, 25);
		btnViewProfile.addActionListener(this);
		frmWorkerMenagment.getContentPane().add(btnViewProfile);
		
		btnMenagePositon = new JButton("Zarzadzaj stanowiskiem");
		btnMenagePositon.setEnabled(false);
		btnMenagePositon.setBounds(327, 225, 170, 25);
		btnMenagePositon.addActionListener(this);
		frmWorkerMenagment.getContentPane().add(btnMenagePositon);
		
		workerList = new JList(listOfWorkreModel);
		
		workerList.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent arg0) {
				int index = workerList.getSelectedIndex();
				if(index>=0){
				String[] buff = listOfWorkreModel.get(index).toString().split(" ");
				for (Worker worker : listOfWorker) {
					if(worker.getId()==Long.parseLong(buff[0])) 
					selectedWorker = worker;
				}
				
				btnSeeSalary.setEnabled(true);
				btnMenagePositon.setEnabled(true);
				btnRemoveWorker.setEnabled(true);
				btnViewProfile.setEnabled(true);
				}
				else if(index<=0){
					btnSeeSalary.setEnabled(false);
					btnMenagePositon.setEnabled(false);
					btnRemoveWorker.setEnabled(false);
					btnViewProfile.setEnabled(false);
				}
				
			}
		});
		
		workerList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		workerList.setValueIsAdjusting(true);
		workerList.setBounds(12, 34, 259, 432);
		frmWorkerMenagment.getContentPane().add(workerList);
		
	}
	
	

	@Override
	public void actionPerformed(ActionEvent arg0)
	{
		if(arg0.getSource()==btnAddWorker) {
			addWorker = new AddWorker("Add Worker",listOfWorkreModel,listOfWorker);
			addWorker.setVisible(true);
			btnSeeSalary.setEnabled(false);
			btnMenagePositon.setEnabled(false);
			btnRemoveWorker.setEnabled(false);
			btnViewProfile.setEnabled(false);

		}
		
		if(arg0.getSource()==btnRemoveWorker) {
			
			Object[] answersOption ={"Tak","Nie"};
			
			int answer = JOptionPane.showOptionDialog(this, "czy napewno chcesz usunąć pracownika o imieniu: "+selectedWorker.getName(),"Usuń pracownika",
					JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,answersOption,answersOption[0]);
			if(answer ==0){
				if(connector.RemoveWorker(selectedWorker.getId())){
					workerList.setSelectedIndex(-1);
					listOfWorkreModel.removeElement(selectedWorker.getId()+" "+selectedWorker.getName()+" "+selectedWorker.getSurname()+" "+selectedWorker.getPosition());
					listOfWorker.remove(selectedWorker);
				}
				else JOptionPane.showMessageDialog(this, "Wystopił problem żądanie nie zostało zrealizowane");
			}
		}
		if(arg0.getSource()==btnViewProfile) {
			ViewProfile profile = new ViewProfile("Profil "+selectedWorker.getSurname(), selectedWorker,listOfWorker,listOfWorkreModel);
			profile.setVisible(true);
		}
		
		if(arg0.getSource()==btnSeeSalary) {
			float result = connector.GetSallary(selectedWorker.getId(), selectedWorker.getPosition());
			if(result!=0.0){
				JOptionPane.showMessageDialog(this, "Pensja pracownika wynosi: "+result);
			}
		}
		
		if(arg0.getSource()==btnMenagePositon) {
			for(int i=0;i<listOfWorkreModel.size();i++)
			{
				String[] buff = listOfWorkreModel.get(i).toString().split(" ");
				if(selectedWorker.getId()==Long.parseLong(buff[0])){
					PossitionMenagment possition= new PossitionMenagment("Zmiana stanowiska",listOfWorker,listOfWorkreModel,i);
					possition.setVisible(true);
				}
			}
		}
		
	}
}
